package com.tt.plat8.bootstrap;

import org.osgi.framework.*;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

import javax.servlet.ServletException;

/**
 * Created with IntelliJ IDEA.
 * UserBean: tangtao
 * Date: 14-1-21
 * Time: 下午2:33
 * To change this template use File | Settings | File Templates.
 */
public class Activator implements BundleActivator, ServiceTrackerCustomizer, BundleListener {

    private static BundleContext context;

    private ResourceProvider resourceProvider;

    static BundleContext getContext() {
        return context;
    }

    // used to track the HttpService
    private ServiceTracker tracker;
    // used to register servlets
    private HttpService httpService;

    @Override
    public void start(BundleContext bundleContext) throws Exception {
        //To change body of implemented methods use File | Settings | File Templates.
        Activator.context = bundleContext;

        resourceProvider = new ResourceProvider();

        bundleContext.addBundleListener(this);



        tracker = new ServiceTracker(bundleContext, HttpService.class, this);
        tracker.open();
    }

    @Override
    public void stop(BundleContext bundleContext) throws Exception {
        //To change body of implemented methods use File | Settings | File Templates.
        Activator.context = null;

        tracker.close();
        tracker = null;

        resourceProvider = null;

        bundleContext.removeBundleListener(this);

        Activator.context = null;
    }

    @Override
    public HttpService addingService(ServiceReference serviceReference) {
        httpService = (HttpService) getContext().getService(serviceReference);

        try {
            // register the servlet at the http service
            httpService.registerServlet("/", new ApplicationServlet(), null,
                    null);

            httpService.registerResources(ResourceProvider.RESOURCE_BASE,ResourceProvider.RESOURCE_BASE,resourceProvider);

        } catch (ServletException e) {
            e.printStackTrace();
        } catch (NamespaceException e) {
            e.printStackTrace();
        }


        return httpService;
    }

    @Override
    public void modifiedService(ServiceReference serviceReference, Object o) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void removedService(ServiceReference serviceReference, Object o) {
        //To change body of implemented methods use File | Settings | File Templates.
        httpService.unregister("/");
    }

    @Override
    public void bundleChanged(BundleEvent event) {
        //To change body of implemented methods use File | Settings | File Templates.
        String name = event.getBundle().getSymbolicName();
        if (name.startsWith("com.vaadin")) {
            if (event.getType() == BundleEvent.STARTED) {
                resourceProvider.add(event.getBundle());
            } else if (event.getType() == BundleEvent.STOPPED) {
                resourceProvider.remove(event.getBundle());
            }
        }
    }
}
